package dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import model.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.*;

public class HibernateAdminDao extends GenericHibernateDAO<Admin, Long> implements AdminDAO {

    //public Admin getById(Long id) {
        //return (Admin) getHibernateTemplate().get(Admin.class, id);
    //}

    //public void save(Admin admin) {
        //getHibernateTemplate().saveOrUpdate(admin);
    //}
 
    //@SuppressWarnings({"unchecked"})
    //public List<Admin> getAll() {
        //return getHibernateTemplate().find("from Admin");
    //}
    
    //public void delete(Long id){
    	//Object a = getHibernateTemplate().load(Admin.class, id);
    	//this.getHibernateTemplate().delete(a);  
    //}
	
	public Admin getByName(final String name) {
        return (Admin) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createQuery("from Admin where name = :name").
                        setParameter("name", name).
                        uniqueResult();
            }
        });
    }
	
    public Admin getByNamePwd(final String name,final String passw) {
        return (Admin) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createQuery("from Admin where name = :name and pwd = :passw").
                        setParameter("name", name).
                        setParameter("passw", passw).
                        uniqueResult();
            }
        });
    }
}
